package com.beeantec.jnrerc.modules.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.beeantec.jnrerc.core.annon.LogInject;
import com.beeantec.jnrerc.core.api.ApiRest;
import com.beeantec.jnrerc.core.api.controller.BaseController;
import com.beeantec.jnrerc.core.enums.DynamicEnum;
import com.beeantec.jnrerc.core.enums.PlanStateEnum;
import com.beeantec.jnrerc.dto.MemberVo;
import com.beeantec.jnrerc.dto.response.DynamicRespDTO;
import com.beeantec.jnrerc.dto.response.MissionRecordRespDTO;
import com.beeantec.jnrerc.entity.*;
import com.beeantec.jnrerc.modules.comment.service.CommentService;
import com.beeantec.jnrerc.modules.comment.service.MemberLikeService;
import com.beeantec.jnrerc.modules.course.service.CourseService;
import com.beeantec.jnrerc.modules.member.service.DynamicService;
import com.beeantec.jnrerc.modules.member.service.MemberService;
import com.beeantec.jnrerc.modules.mission.service.MissionService;
import com.beeantec.jnrerc.modules.user.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 *
 * @author Anthony
 */
@Api(tags={"工作圈"})
@RestController
@RequestMapping("/api/dynamic")
public class DynamicController extends BaseController {
    @Autowired
    private DynamicService dynamicService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private MissionService missionService;



    @Autowired
    private CommentService commentService;


    @Autowired
    private MemberLikeService memberLikeService;

    @Autowired
    private CourseService courseService;
    /**
     * 获取工作圈列表
     * @return
     */
    @LogInject(title = "获取工作圈列表")
    @CrossOrigin
    @ApiOperation(value = "获取工作圈列表")
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public ApiRest<List<DynamicRespDTO>> record(@RequestHeader("token") String token, @RequestParam("type") Integer type,
                                                @RequestParam("page") Integer pageNum,@RequestParam("size") Integer size) {
        QueryWrapper<MemberDynamic> courseDTOQueryWrapper ;
        MemberVo memberVo = memberService.token(token);
        if (type.equals(DynamicEnum.BIDDING.getCode())||type.equals(DynamicEnum.REWARD.getCode())||type.equals(DynamicEnum.COURSE.getCode())||type.equals(DynamicEnum.NEWS.getCode())){
            courseDTOQueryWrapper  = new QueryWrapper<>();
            courseDTOQueryWrapper.lambda()
            .eq(MemberDynamic::getType,type).orderByDesc(MemberDynamic::getCreateTime);
        }else {
            courseDTOQueryWrapper  = new QueryWrapper<>();
            courseDTOQueryWrapper.lambda()
            .orderByDesc(MemberDynamic::getCreateTime);
        }
        Page<MemberDynamic> page = new Page<>(pageNum, size);
        IPage<MemberDynamic> iPage= dynamicService.getBaseMapper().selectPage(page,courseDTOQueryWrapper);
        List<MemberDynamic> dynamicList =iPage.getRecords();
        List<DynamicRespDTO> dynamicRespDTOS = new ArrayList<>();

        for (MemberDynamic dynamic:dynamicList){
            QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
            QueryWrapper<MemberLike> memberLikeQueryWrapper = new QueryWrapper<>();
            QueryWrapper<MemberLike> hasLikeQueryWrapper = new QueryWrapper<>();
            if (dynamic.getType().equals(DynamicEnum.COURSE.getCode())){
                DynamicRespDTO<Course> dynamicRespDTO = new DynamicRespDTO();
                BeanUtils.copyProperties(dynamic,dynamicRespDTO);
                Course course = courseService.getById(dynamic.getItemId());
                commentQueryWrapper.lambda().eq(Comment::getAnswerId,dynamic.getId())
                        .eq(Comment::getAnswerType,2);
                memberLikeQueryWrapper.lambda().eq(MemberLike::getItemId,dynamic.getId())
                        .eq(MemberLike::getType,2);
                dynamicRespDTO.setCommentNum(commentService.count(commentQueryWrapper));
                dynamicRespDTO.setLikeNum(memberLikeService.count(memberLikeQueryWrapper));
                hasLikeQueryWrapper.lambda().eq(MemberLike::getMemberId,memberVo.getMemberId())
                        .eq(MemberLike::getItemId,dynamic.getId())
                        .eq(MemberLike::getType,2);
                dynamicRespDTO.setHasLike(memberLikeService.count(hasLikeQueryWrapper));
                Member member = memberService.getById(dynamicRespDTO.getMemberId().intValue());
                dynamicRespDTO.setDetail(course);
                dynamicRespDTO.setHead(member.getAvatarUrl());
                dynamicRespDTO.setNickName(member.getNickName());
                dynamicRespDTOS.add(dynamicRespDTO);
            }else {
                DynamicRespDTO<Mission> dynamicRespDTO = new DynamicRespDTO();
                BeanUtils.copyProperties(dynamic,dynamicRespDTO);
                Mission mission = missionService.getById(dynamic.getItemId());
                commentQueryWrapper.lambda().eq(Comment::getAnswerId,dynamic.getId())
                        .eq(Comment::getAnswerType,2);
                memberLikeQueryWrapper.lambda().eq(MemberLike::getItemId,dynamic.getId())
                        .eq(MemberLike::getType,2);
                dynamicRespDTO.setCommentNum(commentService.count(commentQueryWrapper));
                dynamicRespDTO.setLikeNum(memberLikeService.count(memberLikeQueryWrapper));
                hasLikeQueryWrapper.lambda().eq(MemberLike::getMemberId,memberVo.getMemberId())
                        .eq(MemberLike::getItemId,dynamic.getId())
                        .eq(MemberLike::getType,2);
                dynamicRespDTO.setHasLike(memberLikeService.count(hasLikeQueryWrapper));
                Member member = memberService.getById(dynamicRespDTO.getMemberId());
                dynamicRespDTO.setDetail(mission);
                dynamicRespDTO.setHead(member.getAvatarUrl());
                dynamicRespDTO.setNickName(member.getNickName());
                dynamicRespDTOS.add(dynamicRespDTO);
            }
        }
        return success(dynamicRespDTOS);

    }
}
